package org.example.http.demo;

import org.example.http.*;

import java.io.IOException;
import java.util.Arrays;

/**
 * 问题: Servlet 何时被创建
 *   答: 当第一次请求该Servlet时, 才会创建该Servlet对象
 *      后续的请求, 都不会再创建新的Servlet对象, 而是直接调用doGet方法
 */

@WebServlet("/hello")
public class HelloServlet extends HttpServletImpl {

    @Override
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws IOException, ServletException {
        String[] names = request.getParameterValues("name");
        String name = "World";
        if(names != null){
            name = Arrays.asList(names).toString();
        }

        // 该行代码必须去掉
        // super.doGet(request, response);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("<html><body><h1>Hello "+name+"</h1></body></html>");
        response.getWriter().flush();
    }

    @Override
    public void init() {
        System.out.println("------------ HelloServlet init");
    }
}
